//
//  SEPrescriptionPickDiagnosisViewController.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/5/11.
//

import UIKit

class SEPrescriptionPickDiagnosisViewController: SEBaseViewController {
    
    var selectedHandler: ((SEPrescriptionDiagnosisPickViewModel) -> Void)?
    var confirmHandler: ((SEPrescriptionDrugPickViewModel) -> Void)?
    
    private var viewModel = SEPrescriptionDiagnosisPickViewModel()
    
    convenience init(type: SEPrescriptionDiagnosisType, selectedHandler: ((SEPrescriptionDiagnosisPickViewModel) -> Void)?) {
        self.init()
        self.viewModel.type = type
        self.selectedHandler = selectedHandler
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        diagnosisListView.viewModel = viewModel
        diagnosisListView.selectedHandler = selectedHandler
        diagnosisListView.dismissHandler = { [weak self] in
            self?.dismissVc()
        }
    }
    
    override func setupUI() {
        view.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        let height = viewModel.type == .diagnose ? UIScreen.screenHeight * 0.85 : UIScreen.screenWidth * 0.85
        view.addSubview(diagnosisListView)
        diagnosisListView.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(height)
            make.centerY.equalToSuperview()
        }
    }
    
    override func setupNavBar() {
        super.setupNavBar()
        
        navigationController?.navBarStyle(.clear)
    }

    // MARK: - getter
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(dismissVc))
        view.addGestureRecognizer(tap)
        
        return view
    }()
    
    private lazy var diagnosisListView: SEPrescriptionPickDiagnosisListView = {
        let view = UINib.loadViewFrom(SEPrescriptionPickDiagnosisListView.self)
        view.backgroundColor = .white
        view.layer.cornerRadius = 16
        view.layer.masksToBounds = true
        return view
    }()
}

extension SEPrescriptionPickDiagnosisViewController {
    @objc private func dismissVc() {
        dismiss(animated: true, completion: nil)
    }
}
